package com.equipment;

import java.util.NoSuchElementException;

import android.annotation.SuppressLint;
import android.util.Log;

import com.util.EncodingUtil;

/**
 * 激活OBU响应幀 BB
 *
 * @version 1.18
 * @author 刘亚东
 */
@SuppressLint("DefaultLocale")
public class ActivateOBUResponse {
	// 帧开始标记
	private String stx = "55AA";
	// 串口帧序列号
	private String rsctl = "00";
	// 长度
	private String Length = "";
	// 代码类型
	private String code = "DF";
	// 拓展子码
	private String subCode = "BB";
	// 异或校验值
	private String bcc = "";

	// 0表示找到OBU，非0表示未找到OBU(长度1)
	private String Status = "";
	// 标签密钥版本(1字节)
	private String ContractVersion = "";
	// 标签号码(8字节)
	private String ContractSerialNumber = "";
	// 标签内车牌号(12字节)
	private String VehicleNumber = "";
	// 车牌颜色(2字节)
	private String VehicleColor = "";
	// 车型(1字节)
	private String VehicleClass = "";
	// 卡类型(1字节)
	private String CardType = "";
	// 卡片密钥版本(1字节)
	private String CardVersion = "";
	// 卡片网络编号(2字节)
	private String CardNetNumber = "";
	// 卡号(8字节)
	private String CardNumber = "";
	// 卡片有效期(4字节)
	private String StartTime="";
	// 卡片失效期(4字节)
	private String StopTime="";
	// 卡内车牌号(12字节)
	private String CardVehicleNumber = "";
	// 信息位标志，Bit0：写系统信息（0：无，1：有）；Bit1：写车辆信息（0：无，1：有）；
	// （注：只有金逸V60标签存在车辆信息，其它无写车辆信息）
	private String InfoFlag = "";
	// 写系统信息分散因子(8字节)
	private String WriteSysFactor = "";
	// 写系统信息随机数(4字节)
	private String WriteSysRand = "00000000";
	// 写系统信息指令长度(1字节)
	private String WriteSysInfoLen = "";
	// 写系统信息指令（0x11,0x12,0x16为18字节，金逸V60为36字节，聚利03为41字节）
	private String WriteSysInfo = "";
	// 写车辆信息随机数(4字节)
	private String WriteVehRand = "00000000";
	// 写车辆信息指令长度(1字节)
	private String WriteVehInfoLen = "";
	// 写车辆信息指令（只有金逸V60标签存在车辆信息，其它无写车辆信息）
	private String WriteVehInfo = "A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5";

	/**
	 * 接受解析激活OBU响应幀BB
	 *
	 * @param context
	 * @return
	 */
	public ActivateOBUResponse analysiss(String context)
			throws NoSuchElementException {
		//55AA060033DFBB00121101090940001600BEA94138383838380000000000011117101101071623000000278000000000000000000000000000F9
		ActivateOBUResponse ar = null;
		// 判断是否是BB指令
		if (!context.substring(12, 14).toUpperCase().equals("BB")) {
			throw new NoSuchElementException("接受类型错误!");
		}
		// 判断状态是否成功
		if (!context.substring(14, 16).equals("00")) {
			throw new NoSuchElementException(
					EncodingUtil.getErrorResult(EncodingUtil
							.hexStringToInt(context.substring(14, 16)) + ""));
		}
		// 截取到BCC校验值 需要判断校验是否相等
		bcc = context.substring(context.length() - 2).toUpperCase();
		// 截取到发送的数据(总长度-2个字节计算到数据的最后位置)
		String str = context.substring(4, context.length() - 2);
		// 得到比对校验
		str = EncodingUtil.yihuo(EncodingUtil.change(str)).toUpperCase();
		// 判断校验是否一致
		if (!str.equals(bcc)) {
			throw new NoSuchElementException("异或错误!");
		}
		// 没有问题就把数据封装到对象中
		ar = new ActivateOBUResponse();
		// 解析串口帧序列号
		rsctl = context.substring(4, 6);
		ar.setRsctl(rsctl);
		// 解析长度
		Length = context.substring(6, 10);
		ar.setLength(Length);
		// 解析代码类型
		code = context.substring(10, 12);
		ar.setCode(code);
		// 解析拓展子代码
		subCode = context.substring(12, 14);
		ar.setSubCode(subCode);
		// 截取状态
		Status = context.substring(14, 16);
		ar.setStatus(Status);
		// 截取标签密钥版本
		ContractVersion = context.substring(16, 18);
		ar.setContractVersion(ContractVersion);
		// 截取标签号码
		ContractSerialNumber = context.substring(18, 34);
		ar.setContractSerialNumber(ContractSerialNumber);
		// 截取标签内车牌号
		VehicleNumber = context.substring(34, 58);
		ar.setVehicleNumber(VehicleNumber);

		// 截取车牌颜色
		VehicleColor = context.substring(58, 62);
		ar.setVehicleColor(VehicleColor);

		// 解析车型
		VehicleClass = context.substring(62, 64);
		ar.setVehicleClass(VehicleClass);

		// 解析卡类型
		CardType = context.substring(64, 66);
		ar.setCardType(CardType);

		// 解析卡片密钥版本
		CardVersion = context.substring(66, 68);
		ar.setCardVersion(CardVersion);

		// 解析卡片网络编号
		CardNetNumber = context.substring(68, 72);
		ar.setCardNetNumber(CardNetNumber);

		// 解析卡号
		CardNumber = context.substring(72, 88);
		ar.setCardNumber(CardNumber);

		// 解析卡片有效期
		StartTime = context.substring(88, 96);
		ar.setStartTime(StartTime);

		// 解析卡片失效期
		StopTime = context.substring(96, 104);
		ar.setStopTime(StopTime);

		// 解析卡内车牌号
		CardVehicleNumber = context.substring(104, 128);
		ar.setCardVehicleNumber(CardVehicleNumber);

		// 截取信息位标志
		InfoFlag = context.substring(128, 130);
		ar.setInfoFlag(InfoFlag);

		// 信息位标志，Bit0：系统信息（0：无，1：有）；Bit1：车辆信息（0：无，1：有）；Bit2：CPU卡15文件信息（0：无，1：有）
		int i = Integer.parseInt(InfoFlag, 16);
		String flag = EncodingUtil.jointZero(Integer.toBinaryString(i),8);
		Log.e("信息位标识：", flag);
		int len=0;
		// 判断是否有系统信息
		if (flag.substring(7).equals("0")) {
			Log.e("写系统信息：", "没有");
			ar.setWriteSysInfoLen("0");
		} else {
			// 解析写系统信息分散因子
			WriteSysFactor = context.substring(130,146);
			ar.setWriteSysFactor(WriteSysFactor);

			// 解析写系统信息随机数
			WriteSysRand = context.substring(146, 154);
			ar.setWriteSysRand(WriteSysRand);

			// 解析写系统信息指令长度
			WriteSysInfoLen = context.substring(154,156);
			ar.setWriteSysInfoLen(WriteSysInfoLen);
			// 得到系统信息长度
			len = Integer.parseInt(EncodingUtil.hexToDec(WriteSysInfoLen, 2));
			len=156+(len*2);
			// 解析写系统信息指令
			WriteSysInfo = context.substring(156,len);
			ar.setWriteSysInfo(WriteSysInfo);
		}
		// 判断是否有车辆信息
		if (flag.substring(6, 7).equals("0")) {
			Log.e("写车辆信息：", "没有");
			ar.setWriteVehInfoLen("0");
		} else {
			if (len==0){
				// 截取车辆信息随机数
				WriteVehRand = context.substring(130,138);
				ar.setWriteVehRand(WriteVehRand);
				// 截取写车辆信息指令长度
				WriteVehInfoLen = context.substring(138,140);
				ar.setWriteVehInfoLen(WriteVehInfoLen);
				// 得到写车辆信息长度
				len=Integer.parseInt(EncodingUtil.hexToDec(WriteVehInfoLen,2));
				len=140+(len*2);
				// 截取写车辆信息
				WriteVehInfo = context.substring(140, len);
				ar.setWriteVehInfo(WriteVehInfo);
			} else {
				// 截取车辆信息随机数
				WriteVehRand = context.substring(len, len+=8);
				ar.setWriteVehRand(WriteVehRand);
				// 截取写车辆信息指令长度
				WriteVehInfoLen = context.substring(len,len+=2);
				ar.setWriteVehInfoLen(WriteVehInfoLen);
				// 得到写车辆信息长度
				int len1=Integer.parseInt(EncodingUtil.hexToDec(WriteVehInfoLen,2));
				len1=len+(len1*2);
				// 截取写车辆信息
				WriteVehInfo = context.substring(len, len1);
				ar.setWriteVehInfo(WriteVehInfo);
			}
		}
		return ar;
	}

	public String getStx() {
		return stx;
	}

	public void setStx(String stx) {
		this.stx = stx;
	}

	public String getRsctl() {
		return rsctl;
	}

	public void setRsctl(String rsctl) {
		this.rsctl = rsctl;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getSubCode() {
		return subCode;
	}

	public void setSubCode(String subCode) {
		this.subCode = subCode;
	}

	public String getBcc() {
		return bcc;
	}

	public void setBcc(String bcc) {
		this.bcc = bcc;
	}

	public String getStatus() {
		return Status;
	}

	public void setStatus(String status) {
		Status = status;
	}

	public String getInfoFlag() {
		return InfoFlag;
	}

	public void setInfoFlag(String infoFlag) {
		InfoFlag = infoFlag;
	}

	public String getLength() {
		return Length;
	}

	public void setLength(String length) {
		Length = length;
	}

	public String getContractVersion() {
		return ContractVersion;
	}

	public void setContractVersion(String contractVersion) {
		ContractVersion = contractVersion;
	}

	public String getContractSerialNumber() {
		return ContractSerialNumber;
	}

	public void setContractSerialNumber(String contractSerialNumber) {
		ContractSerialNumber = contractSerialNumber;
	}

	public String getVehicleNumber() {
		return VehicleNumber;
	}

	public void setVehicleNumber(String vehicleNumber) {
		VehicleNumber = vehicleNumber;
	}

	public String getVehicleColor() {
		return VehicleColor;
	}

	public void setVehicleColor(String vehicleColor) {
		VehicleColor = vehicleColor;
	}

	public String getVehicleClass() {
		return VehicleClass;
	}

	public void setVehicleClass(String vehicleClass) {
		VehicleClass = vehicleClass;
	}

	public String getCardType() {
		return CardType;
	}

	public void setCardType(String cardType) {
		CardType = cardType;
	}

	public String getCardVersion() {
		return CardVersion;
	}

	public void setCardVersion(String cardVersion) {
		CardVersion = cardVersion;
	}

	public String getCardNetNumber() {
		return CardNetNumber;
	}

	public void setCardNetNumber(String cardNetNumber) {
		CardNetNumber = cardNetNumber;
	}

	public String getCardNumber() {
		return CardNumber;
	}

	public void setCardNumber(String cardNumber) {
		CardNumber = cardNumber;
	}

	public String getCardVehicleNumber() {
		return CardVehicleNumber;
	}

	public void setCardVehicleNumber(String cardVehicleNumber) {
		CardVehicleNumber = cardVehicleNumber;
	}

	public String getWriteSysFactor() {
		return WriteSysFactor;
	}

	public void setWriteSysFactor(String writeSysFactor) {
		WriteSysFactor = writeSysFactor;
	}

	public String getWriteSysRand() {
		return WriteSysRand;
	}

	public void setWriteSysRand(String writeSysRand) {
		WriteSysRand = writeSysRand;
	}

	public String getWriteSysInfo() {
		return WriteSysInfo;
	}

	public void setWriteSysInfo(String writeSysInfo) {
		WriteSysInfo = writeSysInfo;
	}

	public String getWriteVehRand() {
		return WriteVehRand;
	}

	public void setWriteVehRand(String writeVehRand) {
		WriteVehRand = writeVehRand;
	}

	public String getWriteVehInfoLen() {
		return WriteVehInfoLen;
	}

	public void setWriteVehInfoLen(String writeVehInfoLen) {
		WriteVehInfoLen = writeVehInfoLen;
	}

	public String getWriteVehInfo() {
		return WriteVehInfo;
	}

	public void setWriteVehInfo(String writeVehInfo) {
		WriteVehInfo = writeVehInfo;
	}

	public String getWriteSysInfoLen() {
		return WriteSysInfoLen;
	}

	public void setWriteSysInfoLen(String writeSysInfoLen) {
		WriteSysInfoLen = writeSysInfoLen;
	}

	public String getStartTime() {
		return StartTime;
	}

	public void setStartTime(String startTime) {
		StartTime = startTime;
	}

	public String getStopTime() {
		return StopTime;
	}

	public void setStopTime(String stopTime) {
		StopTime = stopTime;
	}

}
